package cn.kgc.kgde.bookstore.service.impl;

import cn.kgc.kgde.bookstore.dto.CateDTO;
import cn.kgc.kgde.bookstore.entity.Cate;
import cn.kgc.kgde.bookstore.entity.CateAuthor;
import cn.kgc.kgde.bookstore.entity.CatePress;
import cn.kgc.kgde.bookstore.entity.Category;
import cn.kgc.kgde.bookstore.mapper.CateAuthorMapper;
import cn.kgc.kgde.bookstore.mapper.CateMapper;
import cn.kgc.kgde.bookstore.mapper.CatePressMapper;
import cn.kgc.kgde.bookstore.service.CateService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.toolkit.MPJWrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author myt
 * @since 2022-06-13
 */
@Service
public class CateServiceImpl extends ServiceImpl<CateMapper, Cate> implements CateService {

    @Resource
    private CateMapper cateMapper;
    @Resource
    private CateAuthorMapper cateAuthorMapper;
    @Resource
    private CatePressMapper catePressMapper;


    @Override
    public List<CateAuthor> authorList(CateAuthor author) {
        QueryWrapper wrapper = new QueryWrapper();
        List<CateAuthor> authorList =this.cateAuthorMapper.selectList(null);
        return authorList;
    }

    @Override
    public List<CatePress> pressList(CatePress press) {
        List<CatePress> pressList = this.catePressMapper.selectList(null);
        return pressList;
    }

    /**
     * 叶子节点一对多，，商品列表展示，查询
     * @return
     */
    @Override
    public List<CateDTO> cateList() {
        MPJLambdaWrapper<CateDTO> wrapper=MPJWrappers.lambdaJoin();
              wrapper.select(CateAuthor::getAuthor)
                .select(CatePress::getPress)
                      .select(Category::getCateName)
                .innerJoin(CateAuthor.class,CateAuthor::getId,Cate::getAuthor)
                .innerJoin(CatePress.class,CatePress::getId,Cate::getPress)
                .innerJoin(Category.class,Category::getCateId,Cate::getCategory);

        List<CateDTO> cateDTOList = this.cateMapper.selectJoinList(CateDTO.class, wrapper);
        cateDTOList.forEach(System.out::println);


        return cateDTOList;
    }
    @Override
    public List<CateDTO> cateFind(CateDTO cateDTO) {
        System.out.println("cateDTO = " + cateDTO);
        MPJLambdaWrapper<CateDTO> wrapper=MPJWrappers.lambdaJoin();
        if(cateDTO.getAuthor()!=null){
            wrapper
                    .select(CatePress::getPress)
                    .eq(CateAuthor::getAuthor,cateDTO.getAuthor());

        }
        if(cateDTO.getPress()!=null){
            wrapper
                    .select(CateAuthor::getAuthor)
                    .eq(CatePress::getPress,cateDTO.getPress())
                    ;
        }
        if(cateDTO.getCategory()!=null){
            wrapper
                    .select(CateAuthor::getAuthor)
                    .select(CatePress::getPress)
                    .eq(Category::getCateId,cateDTO.getCategory());
        }
        wrapper.innerJoin(CateAuthor.class,CateAuthor::getId,Cate::getAuthor)
                .innerJoin(CatePress.class,CatePress::getId,Cate::getPress)
                .innerJoin(Category.class,Category::getCateId,Cate::getCategory);
        List<CateDTO> cateDTOList = this.cateMapper.selectJoinList(CateDTO.class, wrapper);
        cateDTOList.forEach(System.out::println);
        return cateDTOList;
    }


}
